<?php
/**
 * Create by PhpStorm
 * User: mozongyou
 * Date: 2021/9/26
 * Time: 2:37 下午
 */
/**
 *
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 * @author mozongyou
 * @date 2021-09-26 2:37 下午
 */
declare(strict_types=1);

namespace App\Service\WebSocket\Circle;
//信息流服务
use App\Service\WebSocket\PushService;
use Psr\Container\ContainerInterface;

class MomentIndexService
{
    use PushService;

    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * pc软件首页
     */
    const PC_INDEX_TYPE_PUBLISH_ID = 'pc-moment-index';

    /**
     * app首页
     */
    const APP_INDEX_TYPE_PUBLISH_ID = 'app-global-moment-index';

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * 获取最新的信息流id
     * @param $fd
     * @param $data
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @author mozongyou
     * @date 2021-09-27 5:10 下午
     */
    public function getLastIds($fd, $data)
    {
        $api = $data['api'];
        $requestId = $data['request_id'];
        $params = $data['params'];
        $publishId = $params['publisher_id'];
        $key = '';
        if ($publishId == self::PC_INDEX_TYPE_PUBLISH_ID) {
            $key = config('cache-key.pc_moment_index_ids');
        } elseif ($publishId == self::APP_INDEX_TYPE_PUBLISH_ID) {
            $key = config('cache-key.app_global_moment_index_ids');
        }
        $id = $params['id'];
        $momentIds = [];
        $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
        if ($refreshNoticeIds = $cache->get($key)) {
            foreach ($refreshNoticeIds as $value) {
                if ($value > $id) {
                    $momentIds[] = $value;
                }
            }
        }
        $result = $this->json($api, $requestId, $momentIds);
        $this->sender->push($fd, $result);
    }
}